package conceptmaniax.hotelapp.activities;

import java.text.ParseException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;
import conceptmaniax.hotelapp.config.Constants;
import conceptmaniax.hotelapp.managers.RequestManager;
import conceptmaniax.hotelapp.util.GeneralUtils;
import conceptmaniax.hotelapp.util.database.DataHelper;

public class BasicItemActivity extends Activity {
		
	private DataHelper dataHelper = new DataHelper(this, Constants.DATABASE_NAME);
	protected Button showInfo;
	protected Button showServices;
	protected Button showGuide;
	protected Button showBooking;
	protected Button requests;
	protected Button more_info;
	protected Button more_info_text;
	protected TableLayout callout;
	protected ListView callout_list;
	protected FrameLayout homeScreen;
	protected TextView details;
	protected TextView counter;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.accomodation_menu);
		
		//Go back to HomePage Button
		final Button go_to_home = (Button) findViewById(R.id.homeButton);
			go_to_home.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {
		    	final Intent homePageIntent = new Intent(Constants.HOMEPAGE_ACTIVITY);
		    	startActivity(homePageIntent);
			}
		});
			
		showInfo = (Button) findViewById(R.id.hotel_button);
		showServices = (Button) findViewById(R.id.services_button);
		showGuide = (Button) findViewById(R.id.guide_button);
		showBooking = (Button) findViewById(R.id.booking_button);
		homeScreen = (FrameLayout) findViewById(R.id.accomodation_menu_frameLayout);
		requests = (Button) findViewById(R.id.requestsButton);
		callout = (TableLayout) findViewById(R.id.callout_table);
		callout_list = (ListView) findViewById(R.id.callout_listview);
		counter = (TextView) findViewById(R.id.counter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if ( this.dataHelper != null ) this.dataHelper.close();
		unbindDrawables(findViewById(R.id.accomodation_menu_relativelayout));
	    System.gc();  
	}
	
	private void unbindDrawables(View view) {
        if (view.getBackground() != null) {
        	view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            	unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            ((ViewGroup) view).removeAllViews();
        }
    }
	
	@Override
	protected void onResume() {
		super.onResume();
		SetButtonActivity.getInstance().setRecognition(true);/* vazw mia metavilit rec = true wste na fortwsei to menu
		 														gia ta dwmatia ki oxi to klassiko menou */
		SetButtonActivity.getInstance().setButtons(this, showInfo,
				showServices, showGuide, showBooking, callout, callout_list,
				homeScreen, requests);
		SetButtonActivity.getInstance().autoMenu();
		try {
			int counter = RequestManager.getInstance().getRequestCount();
			this.counter.setText(""+counter);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    // manage message counter
	    if (this.counter.getText().equals("0")) 
	        this.counter.setVisibility(View.INVISIBLE);
	     else
	        this.counter.setVisibility(View.VISIBLE);
	}
	
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {		
		return super.onCreateOptionsMenu(GeneralUtils.createOptionsMenu(this, menu));
	}
	
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		GeneralUtils.onOptionsItemSelected(this, item);
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected Dialog onCreateDialog(final int id) {
		return GeneralUtils.createAboutUsDialog(this);
	}
}